package com.gukun.springboot.tdengine.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gukun.springboot.tdengine.domain.common.CommonResult;
import com.gukun.springboot.tdengine.domain.managetag.DbsManagetagInfo;
import com.gukun.springboot.tdengine.domain.mock.DbsMocktask;
import com.gukun.springboot.tdengine.dto.*;
import com.gukun.springboot.tdengine.pojo.DBConfig;
import com.gukun.springboot.tdengine.service.mock.DbsMocktaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RequestMapping("/api/tdengine/mocktask/")
@RestController
public class DbsMocktaskController {
    @Autowired
    private DbsMocktaskService dbsMocktaskService;

    /**
     * 实时数据性能查询
     *
     * @return 实时数据性能查询
     */
    @Operation(summary = "实时数据性能查询", description = "实时数据性能查询")
    @PostMapping("/readRealData")
    public CommonResult<String> readRealData(@RequestBody DbsMocktaskDto dbsMocktaskDto) throws SQLException {
        String result = dbsMocktaskService.readRealData(dbsMocktaskDto);
        return CommonResult.success(result);
    }

    /**
     * 新增任务
     *
     * @return 新增任务结果
     */
    @Operation(summary = "新增任务", description = "新增任务")
    @PostMapping("/addTaskInfo")
    public CommonResult<String> addTaskInfo(@RequestBody DbsMocktaskDto dbsMocktaskDto){
        String result = dbsMocktaskService.addTaskInfo(dbsMocktaskDto);
        if(StringUtils.isNotBlank(result)){
            return CommonResult.error(500,result);
        }
        return CommonResult.success("添加任务成功");
    }
    /**
     * 查询任务
     *
     * @return 任务集合
     */
    @Operation(summary = "查询任务", description = "查询任务")
    @PostMapping("/searchTaskInfo")
    public CommonResult<List<DbsMocktask>> searchTaskInfo(@RequestBody CreateTableDto createTableDto) throws SQLException {
        List<DbsMocktask> list = dbsMocktaskService.searchTaskInfo(createTableDto);
        if(list == null || list.size() == 0){
//            return CommonResult.error(500,"查询任务出错，请联系管理员");
        }
        return CommonResult.success(list);
    }

    /**
     * 删除任务
     *
     * @return 删除任务
     */
    @Operation(summary = "删除任务", description = "删除任务")
    @PostMapping("/deleteTask")
    public CommonResult<String> deleteTask(@RequestBody List<DbsMocktask> params){
        String result = dbsMocktaskService.deleteTask(params);
        if(result== null){
            return CommonResult.error(500,"删除出错，请联系管理员");
        }
        return CommonResult.success("删除成功");
    }

    /**
     * 开启任务
     *
     * @return 开启任务
     */
    @Operation(summary = "开启任务", description = "开启任务")
    @PostMapping("/startTask")
    public CommonResult<String> startTask(@RequestBody List<DbsMocktaskDto> params){
        String result = dbsMocktaskService.startTask(params,null);
        if(result== null){
            return CommonResult.error(500,"开启任务出错，请联系管理员");
        }
        return CommonResult.success("开启任务成功");
    }

    /**
     * 停止任务
     *
     * @return 停止任务
     */
    @Operation(summary = "停止任务", description = "停止任务")
    @PostMapping("/stopTask")
    public CommonResult<String> stopTask(@RequestBody List<DbsMocktaskDto> params){
        String result = dbsMocktaskService.stopTask(params,null);
        if(result== null){
            return CommonResult.error(500,"停止任务出错，请联系管理员");
        }
        return CommonResult.success("停止任务成功");
    }

    /**
     * 开启全部任务
     *
     * @return 开启全部任务
     */
    @Operation(summary = "开启全部任务", description = "开启全部任务")
    @PostMapping("/startAllTask")
    public CommonResult<String> startAllTask(@RequestBody WriteRulesDto writeRulesDto){
        String result = dbsMocktaskService.startAllTask(writeRulesDto);
        if(result== null){
            return CommonResult.error(500,"开启任务出错，请联系管理员");
        }
        return CommonResult.success("开启任务成功");
    }

    /**
     * 停止全部任务
     *
     * @return 停止全部任务
     */
    @Operation(summary = "停止全部任务", description = "停止全部任务")
    @PostMapping("/stopAllTask")
    public CommonResult<String> stopAllTask(@RequestBody WriteRulesDto writeRulesDto){
        String result = dbsMocktaskService.stopAllTask(writeRulesDto);
        if(result== null){
            return CommonResult.error(500,"停止任务出错，请联系管理员");
        }
        return CommonResult.success("停止任务成功");
    }

    /**
     * 历史数据写入执行
     *
     * @return 模拟数据写入执行
     */
    @Operation(summary = "模拟数据写入执行", description = "模拟数据写入执行")
    @PostMapping("/batchInsertInfoWrite")
    public CommonResult<String> batchInsertInfoWrite(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "模拟数据写入执行") @org.springframework.web.bind.annotation.RequestBody WriteRulesDto
                                                           writeRulesDto) throws Exception {
        return dbsMocktaskService.batchInsertInfoWrite(writeRulesDto);
    }

    /**
     * 查询已执行数量
     *
     * @return 查询已执行数量
     */
    @Operation(summary = "查询已执行数量", description = "查询已执行数量")
    @GetMapping("/getDoingCount")
    public CommonResult<String> getDoingCount() {
        return CommonResult.success(String.valueOf(dbsMocktaskService.execCount));
    }
}
